<template>
  <div class="pie" id="bar1"></div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  name: 'Pie',
  props: {
    data: {
      type: Array,
      default: () => {
        return [];
      }
    }
  },
  mounted() {
    const that = this;
    this.$nextTick(() => {
      const chartDom = document.getElementById('bar1');
      const myChart = echarts.init(chartDom);

      const option = {
        tooltip: {
          trigger: 'item'
        },
        title: {
          text: '热词趋势',
          left: '3.8%'
        },
        xAxis: {
          type: 'category',
          data: this.data.map(e => e.name)
        },
        yAxis: {
          type: 'value',
          name: '出现次数(次)',
          nameTextStyle: {
            fontSize: 16,
            align: 'right'
          }
        },
        grid: {
          top: '15%',
          left: '8%',
          right: '5%'
        },
        series: [
          {
            data: that.data,
            type: 'bar',
            barWidth: 34,

            itemStyle: {
              borderRadius: [20, 20, 0, 0],
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0, color: '#4071FB' // 0% 处的颜色
                  },
                  {
                    offset: 0.7, color: 'rgba(75, 148, 255, 0.7)' // 0% 处的颜色
                  },
                  {
                    offset: 1, color: 'rgba(130, 181, 255, 0.1)' // 100% 处的颜色
                  }
                ]
              }
            }
          }
        ]
      };

      option && myChart.setOption(option);
    });
  }
};
</script>

<style scoped>
  .pie {
    height: inherit;
  }
</style>
